package java6681.lesson05;

import java.util.*;

/**
 * @author Junichiro
 * @description 验证课堂小测
 * @date 2021-04-05 19:21
 **/

public class PointDemo6681 {
    public static void main(String[] args) {
        demo1();
        demo2();
        demo3();
    }

    static class Point6681 implements Comparable<Point6681> {
        private int x;
        private int y;

        public Point6681(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public Point6681() {
        }

        public int getX() {
            return x;
        }

        public void setX(int x) {
            this.x = x;
        }

        public int getY() {
            return y;
        }

        public void setY(int y) {
            this.y = y;
        }

        @Override
        public String toString() {
            return "(" + x + "," + y + ")";
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Point6681 point6681 = (Point6681) o;
            return x == point6681.x &&
                    y == point6681.y;
        }

        @Override
        public int hashCode() {
            return Objects.hash(x, y);
        }

        @Override
        public int compareTo(Point6681 p) {
            int num = y - p.y;
            int num2 = num == 0 ? x - p.x : num;
            return num2;
        }
    }

    /**
     * 功能: 验证第1题：List集合的查找与对象相等
     * 原因：重写了equals方法之后可以输出true，比较的是值的内容,contains()方法默认判断引用相等
     */
    public static void demo1() {
        Collection<Point6681> list = new ArrayList<>();
        list.add(new Point6681(1, 2));
        list.add(new Point6681(21, 31));
        list.add(new Point6681(11, 15));
        list.add(new Point6681(1, 2));
        list.add(new Point6681(21, 17));
        list.add(new Point6681(13, 15));
        System.out.println(list.contains(new Point6681(11, 15)));//true
    }

    /**
     * 功能:验证第2题：Set集合排除相同对象的方法
     * 原因：重写了hashCode方法和equals方法之后选A，他们可以把内容相同的去掉。
     *      Point如果没有重写hashCode方法,只要Point对象的地址不同其hashCode()方法的返回值就不一样，
     *      所以代码中创建的Point对象都能添加进HashSet
     */
    public static void demo2() {
        Collection<Point6681> list = new HashSet<>();
        list.add(new Point6681(1, 2));
        list.add(new Point6681(21, 31));
        list.add(new Point6681(11, 15));
        list.add(new Point6681(1, 2));
        list.add(new Point6681(21, 17));
        list.add(new Point6681(13, 15));
        System.out.println(list);//[(1,2), (11,15), (13,15), (21,17), (21,31)]
    }

    /**
     * 功能:验证第3题：TreeSet集合排重、排序的方法
     * 原因：修改代码后选A，这是自然排序，让元素所属的类实现自然排序接口 Comparable，
     *          重写方法compareTo，按照规则排序,去重复,然后加上y的排序
     */
    public static void demo3() {
        Collection<Point6681> list = new TreeSet<>();
        list.add(new Point6681(1, 2));
        list.add(new Point6681(21, 31));
        list.add(new Point6681(11, 15));
        list.add(new Point6681(1, 2));
        list.add(new Point6681(21, 17));
        list.add(new Point6681(13, 15));
        System.out.println(list);//[(1,2), (11,15), (13,15), (21,17), (21,31)]
    }
}
